package com.ruoyi.web.controller.basicInformation;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.system.basicInformation.service.ITTerminalService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.basicInformation.vo.TCarVo;
import com.ruoyi.system.basicInformation.bo.TCarQueryBo;
import com.ruoyi.system.basicInformation.bo.TCarAddBo;
import com.ruoyi.system.basicInformation.bo.TCarEditBo;
import com.ruoyi.system.basicInformation.service.ITCarService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 车辆信息Controller
 *
 * @author ruoyi
 * @date 2021-05-18
 */
@Api(value = "车辆信息控制器", tags = {"车辆信息管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/basicInformation/car")
public class TCarController extends BaseController {

    private final ITCarService iTCarService;

    @Autowired
    private ITTerminalService terminalService;

    /**
     * 查询车辆信息列表
     */
    @ApiOperation("查询车辆信息列表")
    @PreAuthorize("@ss.hasPermi('basicInformation:car:list')")
    @GetMapping("/list")
    public TableDataInfo<TCarVo> list(TCarQueryBo bo) {
        startPage();
        List<TCarVo> list = iTCarService.queryList(bo);
        return getDataTable(list);
    }

    /**
     * 查询车辆信息列表
     */
    @ApiOperation("查询车辆信息列表")
//    @PreAuthorize("@ss.hasPermi('basicInformation:car:list')")
    @GetMapping("/listCarCurrentPoint")
    public TableDataInfo<TCarVo> listCarCurrentPoint(TCarQueryBo bo) {
        startPage();
        List<TCarVo> list = iTCarService.listCarCurrentPoint(bo);
        return getDataTable(list);
    }
    /**
     * 导出车辆信息列表
     */
    @ApiOperation("导出车辆信息列表")
    @PreAuthorize("@ss.hasPermi('basicInformation:car:export')")
    @Log(title = "车辆信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<TCarVo> export(TCarQueryBo bo) {
        List<TCarVo> list = iTCarService.queryList(bo);
        ExcelUtil<TCarVo> util = new ExcelUtil<TCarVo>(TCarVo.class);
        return util.exportExcel(list, "车辆信息");
    }

    /**
     * 获取车辆信息详细信息
     */
    @ApiOperation("获取车辆信息详细信息")
    @PreAuthorize("@ss.hasPermi('basicInformation:car:query')")
    @GetMapping("/{id}")
    public AjaxResult<TCarVo> getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(iTCarService.queryById(id));
    }

    /**
     * 查询是否有车辆即将到期
     */
    @ApiOperation("查询是否有车辆即将到期")
//    @PreAuthorize("@ss.hasPermi('basicInformation:car:list')")
    @GetMapping("/checkCarLimit")
    public AjaxResult<Boolean> checkCarLimit() {
        return AjaxResult.success(iTCarService.checkCarLimit());
    }

    /**
     * 新增车辆信息
     */
    @ApiOperation("新增车辆信息")
    @PreAuthorize("@ss.hasPermi('basicInformation:car:add')")
    @Log(title = "车辆信息", businessType = BusinessType.INSERT)
    @PostMapping()
    public AjaxResult<Void> add(@RequestBody TCarAddBo bo) {
        int result = iTCarService.insertByAddBo(bo) ? 1 : 0;
        //绑定终端
        //terminalService.updateTerminalByObjId(bo.getTerminalId(), bo.getId());
        return toAjax(result);
    }

    /**
     * 解绑终端设备
     */
    @ApiOperation("解绑终端设备")
    @PreAuthorize("@ss.hasPermi('basicInformation:car:edit')")
    @Log(title = "解绑终端设备", businessType = BusinessType.UPDATE)
    @PostMapping("/unBundling")
    public AjaxResult<Void> unBundling(@RequestBody TCarEditBo bo) {
        return toAjax(terminalService.unBundling(bo.getTerminalId()) ? 1 : 0);
    }

    /**
     * 修改车辆信息
     */
    @ApiOperation("修改车辆信息")
    @PreAuthorize("@ss.hasPermi('basicInformation:car:edit')")
    @Log(title = "车辆信息", businessType = BusinessType.UPDATE)
    @PutMapping()
    public AjaxResult<Void> edit(@RequestBody TCarEditBo bo) {
        //绑定终端
        if(bo.getTerminalId()!=bo.getTerminalIdOld())
        {
            terminalService.unBundling(bo.getTerminalIdOld());
        }
        terminalService.updateTerminalByObjId(bo.getTerminalId(), bo.getId());
        return toAjax(iTCarService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除车辆信息
     */
    @ApiOperation("删除车辆信息")
    @PreAuthorize("@ss.hasPermi('basicInformation:car:remove')")
    @Log(title = "车辆信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@PathVariable Long[] ids) {
        for (int i = 0; i < ids.length; i++) {

            TCarVo tCarVo=  iTCarService.queryById(ids[i]);

            terminalService.unBundling(tCarVo.getTerminalId()!=null?tCarVo.getTerminalId().longValue():0);
        }


        return toAjax(iTCarService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
